package com.hljx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hljx.config.exception.DataException;
import com.hljx.dto.AccountingUnitWeightDTO;
import com.hljx.dto.AccountingUnitWeightPageDTO;
import com.hljx.dto.ExportAccountingUnitWeight;
import com.hljx.dto.InheritAccountingUnitWeightDTO;
import com.hljx.entity.AccountingUnitWeight;
import com.hljx.entity.AssessmentCycle;
import com.hljx.entity.SysAccountingUnit;
import com.hljx.excel.ExcelTransfer;
import com.hljx.mapper.sys.AccountingUnitWeightMapper;
import com.hljx.service.AccountingUnitWeightService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hljx.service.AssessmentCycleService;
import com.hljx.vo.AccountingVO;
import com.hljx.vo.SysDictItemVO;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 核算单元权重配置表 服务实现类
 * </p>
 *
 * @author gx
 * @since 2023-10-07
 */
@Service
public class AccountingUnitWeightServiceImpl extends ServiceImpl<AccountingUnitWeightMapper, AccountingUnitWeight> implements AccountingUnitWeightService {

    @Autowired
    private ExcelTransfer<AccountingUnitWeight> excelTransfer;

    @Autowired
    AssessmentCycleService assessmentCycleService;

    @Override
    public List<AccountingVO> listAccountingVO(Long assessmentCycleId) {
        return baseMapper.listAccountingVO(assessmentCycleId);
    }

    @Override
    public Boolean saveAccountingUnitWeight(AccountingUnitWeightDTO dto) {
        AccountingUnitWeight accountingUnitWeight = new AccountingUnitWeight();
        BeanUtil.copyProperties(dto,accountingUnitWeight);
        return save(accountingUnitWeight);
    }

    @Override
    public IPage<AccountingUnitWeight> listAccountingUnitWeightByPage(AccountingUnitWeightPageDTO dto) {
        Page<AccountingUnitWeight> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        return baseMapper.listByPage(page,dto);
    }

    @Override
    public Integer isFile(Long id) {
        return assessmentCycleService.getById(id).getIsFile();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateAccountingUnitWeight(AccountingUnitWeightDTO dto) {
        AccountingUnitWeight accountingUnitWeight = getById(dto.getId());
        accountingUnitWeight.setWorkloadWeight(dto.getWorkloadWeight());
        return saveOrUpdate(accountingUnitWeight);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean inheritAccountingUnitWeight(InheritAccountingUnitWeightDTO dto) {
        List<AccountingUnitWeight> list = this.list(new LambdaQueryWrapper<AccountingUnitWeight>().eq(AccountingUnitWeight::getAssessmentCycleId, dto.getInheritanceId()));
        if(CollectionUtil.isEmpty(list)){
            throw new DataException("继承周期没有数据",201);
        }
        //删除目标周期已经存在的数据
        this.remove(new LambdaQueryWrapper<AccountingUnitWeight>().eq(AccountingUnitWeight::getAssessmentCycleId,dto.getTargetId()));
        ArrayList<AccountingUnitWeight> accountingUnitWeights = new ArrayList<>();
        //继承选中周期核算单元的工作量权重
        for (AccountingUnitWeight entity : list) {
            AccountingUnitWeight accountingUnitWeight = new AccountingUnitWeight();
            accountingUnitWeight.setAccountingName(entity.getAccountingName());
            accountingUnitWeight.setAccountingCode(entity.getAccountingCode());
            accountingUnitWeight.setWorkloadWeight(entity.getWorkloadWeight());
            accountingUnitWeight.setAssessmentCycleId(dto.getTargetId());
            accountingUnitWeights.add(accountingUnitWeight);
        }
        return saveBatch(accountingUnitWeights);
    }

    @Override
    public void export(ExportAccountingUnitWeight dto, HttpServletResponse response) throws ClassNotFoundException {
        List<Long> ids = dto.getIds();
        if(CollectionUtil.isEmpty(ids)){
            throw new DataException("请选中数据",201);
        }
        List<AccountingUnitWeight> list = baseMapper.listAccountingUnitWeight(dto);
        excelTransfer.exportExcel(response, list, "核算单元权重配置", "sheet1", AccountingUnitWeight.class);

    }
}
